package com.jk.hearthstone.core;

import com.jk.hearthstone.card.BaseCard;
import com.jk.hearthstone.card.biology.entourage.BaseEntourage;
import com.jk.hearthstone.card.magic.BaseMagic;
import com.jk.hearthstone.data.Action;
import com.jk.hearthstone.data.Desktop;
import com.jk.hearthstone.data.Turn;

import java.util.ArrayList;
import java.util.List;

import static com.jk.hearthstone.enumeration.ActionType.*;
import static com.jk.hearthstone.enumeration.MagicType.MAGIC_TYPE_WITHOUTTARGET;
import static com.jk.hearthstone.enumeration.SkillType.*;

/**
 * @author jk
 * 行为的生产者
 */
public class Producer {

    /**
     * 获取当前状态下，所有的动作可能
     * @param desktop
     * @return 包含所有信息
     */
    public static Turn getCurrentAction(Desktop desktop){
        Turn turn = new Turn();
        //手牌动作
        turn.actions.addAll(cardAction(desktop));
        //随从动作
        turn.actions.addAll(entourageAction(desktop));
        //英雄攻击
        turn.actions.addAll(heroAttackAction(desktop));
        //英雄技能
        turn.actions.addAll(heroSkillAction(desktop));
        return turn;
    }

    private static List<Action> cardAction(Desktop desktop){
        List<Action> actions = new ArrayList<>();
        //遍历手牌
        for (BaseCard card : desktop.myCards) {
            //费用够的情况
            if(card.cost<=desktop.myself.power){
                //如果是法术牌
                if(card instanceof BaseMagic){
                    //不需要选择目标的法术牌
                    if(((BaseMagic) card).magicType.equals(MAGIC_TYPE_WITHOUTTARGET)){
                        actions.add(new Action(ACTION_TYPE_PLAY,card));
                    }else {
                        //todo:未考虑魔免
                        for (BaseEntourage entourage : desktop.myEntourage) {
                            actions.add(new Action(ACTION_TYPE_PLAY,card,entourage));
                        }
                        for (BaseEntourage entourage : desktop.enemiesEntourage) {
                            actions.add(new Action(ACTION_TYPE_PLAY,card,entourage));
                        }
                        actions.add(new Action(ACTION_TYPE_PLAY,card,desktop.myself));
                        actions.add(new Action(ACTION_TYPE_PLAY,card,desktop.enemy));
                    }
                }
                //如果是随从牌
                else if(card instanceof BaseEntourage){
                    //费用够 且场上的随从没有满
                    if(card.cost<desktop.myself.power && desktop.myEntourage.size()<7){
                        //todo:未考虑战吼需要选择目标的情况
                        actions.add(new Action(ACTION_TYPE_PLAY,card));
                    }
                }
            }
        }
        return actions;
    }

    private static List<Action> entourageAction(Desktop desktop){
        List<Action> actions = new ArrayList<>();
        //遍历随从
        for (BaseEntourage entourage : desktop.myEntourage) {
            //可攻击，且没有被冰冻
            if(entourage.canAttack && !entourage.frozen){
                for (BaseEntourage entourage1 : desktop.enemiesEntourage) {
                    actions.add(new Action(ACTION_TYPE_ATTACK,entourage,entourage1));
                }
                actions.add(new Action(ACTION_TYPE_ATTACK,entourage,desktop.enemy));
            }
        }
        return actions;
    }

    private static List<Action> heroAttackAction(Desktop desktop){
        List<Action> actions = new ArrayList<>();
        //英雄攻击
        if(desktop.myself.canAttack){
            int att = desktop.myself.att;
            if(desktop.myself.arms!=null){
                att += desktop.myself.arms.att;
            }
            //英雄攻击力大于0才能攻击
            if(att>0){
                for (BaseEntourage entourage1 : desktop.enemiesEntourage) {
                    actions.add(new Action(ACTION_TYPE_ATTACK,desktop.myself,entourage1));
                }
                actions.add(new Action(ACTION_TYPE_ATTACK,desktop.myself,desktop.enemy));
            }
        }
        return actions;
    }

    private static List<Action> heroSkillAction(Desktop desktop){
        List<Action> actions = new ArrayList<>();
        //英雄技能 水晶够且可以释放技能的情况下
        if(desktop.myself.power>=desktop.myself.skillCost && desktop.myself.canSkill){
            //英雄技能不需要选择目标的情况
            if(desktop.myself.skillType.equals(SKILL_TYPE_WITHOUTTARGET)){
               actions.add(new Action(ACTION_TYPE_HEROSKILL,desktop.myself));
            }
            //英雄技能需要选择目标的情况
            else if(desktop.myself.skillType.equals(SKILL_TYPE_WITHTARGET)){
                //加入所有可能性
                //todo：还没有考虑魔免
                for (BaseEntourage entourage : desktop.myEntourage) {
                    actions.add(new Action(ACTION_TYPE_HEROSKILL,desktop.myself,entourage));
                }
                for (BaseEntourage entourage : desktop.enemiesEntourage) {
                    actions.add(new Action(ACTION_TYPE_HEROSKILL,desktop.myself,entourage));
                }
                actions.add(new Action(ACTION_TYPE_HEROSKILL,desktop.myself,desktop.myself));
                actions.add(new Action(ACTION_TYPE_HEROSKILL,desktop.myself,desktop.enemy));
            }
        }
        return actions;
    }

}
